import logging
from pathlib import Path

import pandas as pd
import pysnooper

from login import login_ing
from page import get_page
from 判断是否有广告弹窗 import yes_or_no
from 获取聊天数据 import 获取聊天数据并上传ERP
from 获取违规信息并上传EPR import 获取违规并上传ERP

def get_data(port, user_path, down_path, date, usernames):
    # 清空下载文件夹
    for x in range(10):
        try:
            page = get_page(port, user_path, down_path)
            page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.wait(5)
            if page.s_eles('.auxo-btn auxo-btn-primary'):
                page.quit()
                page.wait(3)
                page = get_page(port, user_path, down_path)
                page.get("https://fxg.jinritemai.com/login/common")
            if not page.s_eles(".index_roleItem__1-Hwe") and not (
                    page.s_eles('.feige_headerIcon__3CdVk') or page.s_eles('接待')):
                login_ing(page, usernames)
            if page.s_eles('text=重试'):
                logging.error('出现重试，登录信息异常')
                page.quit()
                continue
            page.wait.ele_deleted('text=加载中', timeout=30)
            店铺eles = page.eles('@class^index_introName')
            店铺名_list = [i.text for i in 店铺eles]
            for 店铺名 in 店铺名_list:
                page.wait.ele_deleted('text=加载中', timeout=10)
                shop_name = '抖音小店-' + 店铺名
                if Path(f"C:\\抖音店铺违规积分\\{date}\\{shop_name}.txt").exists():
                    continue
                logging.info(shop_name)
                page(店铺名).click(by_js=True)
                page.wait.ele_displayed('text=奖惩中心')
                page('text=奖惩中心').click(by_js=True)
                if page.eles('@class^index_introName'):
                    print(shop_name + '没有权限')
                    with open(f"C:\\抖音店铺违规积分\\{date}\\{shop_name}.txt", "w", encoding="utf-8") as f:
                        f.write("没有权限")
                    page.wait(3)
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                page.wait.ele_displayed('text=扣分明细')
                page.eles('text=扣分明细')[0].click(by_js=True)
                page.wait(3)
                page('text=全部扣分罚单').click(by_js=True)
                tab1 = page.get_tab(page.latest_tab)
                tab1.wait(3)
                获取违规并上传ERP(tab1, shop_name)
                page('text=严重违规').click()
                page.wait(3)
                page('text=全部扣分罚单').click(by_js=True)
                tab2 = page.get_tab(page.latest_tab)
                tab2.wait(3)
                获取违规并上传ERP(tab2, shop_name)
                page.wait(1)
                获取聊天数据并上传ERP(page, shop_name)
                page.wait(1)
                with open(f"C:\\抖音店铺违规积分\\{date}\\{shop_name}.txt", "w", encoding="utf-8") as f:
                    f.write("数据获取成功")
                page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.quit()
            return '成功'
        except Exception as e:
            logging.error(e)
            continue
